Passed
Pull Request — master (#47)
by
unknown
05:23
created

collapse.js ➔ _objectSpread   B

Complexity

Conditions 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
c 0
b 0
f 0
dl 18
loc 18
rs 8.6666
1
/*!
2
  * Bootstrap collapse.js v4.6.0 (https://getbootstrap.com/)
3
  * Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
4
  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5
  */
6
(function (global, factory) {
7
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery'), require('./util.js')) :
8
  typeof define === 'function' && define.amd ? define(['jquery', './util'], factory) :
0 ignored issues
show
Bug introduced by
The variable define seems to be never declared. If this is a global, consider adding a /** global: define */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
9
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Collapse = factory(global.jQuery, global.Util));
0 ignored issues
show
Bug introduced by
The variable globalThis seems to be never declared. If this is a global, consider adding a /** global: globalThis */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Best Practice introduced by
If you intend to check if the variable self is declared in the current environment, consider using typeof self === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
Comprehensibility introduced by
Usage of the sequence operator is discouraged, since it may lead to obfuscated code.

The sequence or comma operator allows the inclusion of multiple expressions where only is permitted. The result of the sequence is the value of the last expression.

This operator is most often used in for statements.

Used in another places it can make code hard to read, especially when people do not realize it even exists as a seperate operator.

This check looks for usage of the sequence operator in locations where it is not necessary and could be replaced by a series of expressions or statements.

var a,b,c;

a = 1, b = 1,  c= 3;

could just as well be written as:

var a,b,c;

a = 1;
b = 1;
c = 3;

To learn more about the sequence operator, please refer to the MDN.

Loading history...
10
}(this, (function ($, Util) { 'use strict';
11
12
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
13
14
  var $__default = /*#__PURE__*/_interopDefaultLegacy($);
15
  var Util__default = /*#__PURE__*/_interopDefaultLegacy(Util);
16
17
  function _defineProperties(target, props) {
18
    for (var i = 0; i < props.length; i++) {
19
      var descriptor = props[i];
20
      descriptor.enumerable = descriptor.enumerable || false;
21
      descriptor.configurable = true;
22
      if ("value" in descriptor) descriptor.writable = true;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
23
      Object.defineProperty(target, descriptor.key, descriptor);
24
    }
25
  }
26
27
  function _createClass(Constructor, protoProps, staticProps) {
28
    if (protoProps) _defineProperties(Constructor.prototype, protoProps);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
29
    if (staticProps) _defineProperties(Constructor, staticProps);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
30
    return Constructor;
31
  }
32
33
  function _extends() {
34
    _extends = Object.assign || function (target) {
0 ignored issues
show
Comprehensibility introduced by
It seems like you are trying to overwrite a function name here. _extends is already defined in line 33 as a function. While this will work, it can be very confusing.
Loading history...
35
      for (var i = 1; i < arguments.length; i++) {
36
        var source = arguments[i];
37
38
        for (var key in source) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
39
          if (Object.prototype.hasOwnProperty.call(source, key)) {
40
            target[key] = source[key];
41
          }
42
        }
43
      }
44
45
      return target;
46
    };
47
48
    return _extends.apply(this, arguments);
49
  }
50
51
  /**
52
   * ------------------------------------------------------------------------
53
   * Constants
54
   * ------------------------------------------------------------------------
55
   */
56
57
  var NAME = 'collapse';
58
  var VERSION = '4.6.0';
59
  var DATA_KEY = 'bs.collapse';
60
  var EVENT_KEY = "." + DATA_KEY;
61
  var DATA_API_KEY = '.data-api';
62
  var JQUERY_NO_CONFLICT = $__default['default'].fn[NAME];
63
  var Default = {
64
    toggle: true,
65
    parent: ''
66
  };
67
  var DefaultType = {
68
    toggle: 'boolean',
69
    parent: '(string|element)'
70
  };
71
  var EVENT_SHOW = "show" + EVENT_KEY;
72
  var EVENT_SHOWN = "shown" + EVENT_KEY;
73
  var EVENT_HIDE = "hide" + EVENT_KEY;
74
  var EVENT_HIDDEN = "hidden" + EVENT_KEY;
75
  var EVENT_CLICK_DATA_API = "click" + EVENT_KEY + DATA_API_KEY;
76
  var CLASS_NAME_SHOW = 'show';
77
  var CLASS_NAME_COLLAPSE = 'collapse';
78
  var CLASS_NAME_COLLAPSING = 'collapsing';
79
  var CLASS_NAME_COLLAPSED = 'collapsed';
80
  var DIMENSION_WIDTH = 'width';
81
  var DIMENSION_HEIGHT = 'height';
82
  var SELECTOR_ACTIVES = '.show, .collapsing';
83
  var SELECTOR_DATA_TOGGLE = '[data-toggle="collapse"]';
84
  /**
85
   * ------------------------------------------------------------------------
86
   * Class Definition
87
   * ------------------------------------------------------------------------
88
   */
89
90
  var Collapse = /*#__PURE__*/function () {
91
    function Collapse(element, config) {
92
      this._isTransitioning = false;
93
      this._element = element;
94
      this._config = this._getConfig(config);
95
      this._triggerArray = [].slice.call(document.querySelectorAll("[data-toggle=\"collapse\"][href=\"#" + element.id + "\"]," + ("[data-toggle=\"collapse\"][data-target=\"#" + element.id + "\"]")));
96
      var toggleList = [].slice.call(document.querySelectorAll(SELECTOR_DATA_TOGGLE));
97
98
      for (var i = 0, len = toggleList.length; i < len; i++) {
99
        var elem = toggleList[i];
100
        var selector = Util__default['default'].getSelectorFromElement(elem);
101
        var filterElement = [].slice.call(document.querySelectorAll(selector)).filter(function (foundElem) {
102
          return foundElem === element;
103
        });
104
105
        if (selector !== null && filterElement.length > 0) {
106
          this._selector = selector;
107
108
          this._triggerArray.push(elem);
109
        }
110
      }
111
112
      this._parent = this._config.parent ? this._getParent() : null;
113
114
      if (!this._config.parent) {
115
        this._addAriaAndCollapsedClass(this._element, this._triggerArray);
116
      }
117
118
      if (this._config.toggle) {
119
        this.toggle();
120
      }
121
    } // Getters
122
123
124
    var _proto = Collapse.prototype;
125
126
    // Public
127
    _proto.toggle = function toggle() {
128
      if ($__default['default'](this._element).hasClass(CLASS_NAME_SHOW)) {
129
        this.hide();
130
      } else {
131
        this.show();
132
      }
133
    };
134
135
    _proto.show = function show() {
136
      var _this = this;
137
138
      if (this._isTransitioning || $__default['default'](this._element).hasClass(CLASS_NAME_SHOW)) {
139
        return;
140
      }
141
142
      var actives;
143
      var activesData;
144
145
      if (this._parent) {
146
        actives = [].slice.call(this._parent.querySelectorAll(SELECTOR_ACTIVES)).filter(function (elem) {
147
          if (typeof _this._config.parent === 'string') {
148
            return elem.getAttribute('data-parent') === _this._config.parent;
149
          }
150
151
          return elem.classList.contains(CLASS_NAME_COLLAPSE);
152
        });
153
154
        if (actives.length === 0) {
155
          actives = null;
156
        }
157
      }
158
159
      if (actives) {
160
        activesData = $__default['default'](actives).not(this._selector).data(DATA_KEY);
161
162
        if (activesData && activesData._isTransitioning) {
163
          return;
164
        }
165
      }
166
167
      var startEvent = $__default['default'].Event(EVENT_SHOW);
168
      $__default['default'](this._element).trigger(startEvent);
169
170
      if (startEvent.isDefaultPrevented()) {
171
        return;
172
      }
173
174
      if (actives) {
175
        Collapse._jQueryInterface.call($__default['default'](actives).not(this._selector), 'hide');
176
177
        if (!activesData) {
178
          $__default['default'](actives).data(DATA_KEY, null);
179
        }
180
      }
181
182
      var dimension = this._getDimension();
183
184
      $__default['default'](this._element).removeClass(CLASS_NAME_COLLAPSE).addClass(CLASS_NAME_COLLAPSING);
185
      this._element.style[dimension] = 0;
186
187
      if (this._triggerArray.length) {
188
        $__default['default'](this._triggerArray).removeClass(CLASS_NAME_COLLAPSED).attr('aria-expanded', true);
189
      }
190
191
      this.setTransitioning(true);
192
193
      var complete = function complete() {
194
        $__default['default'](_this._element).removeClass(CLASS_NAME_COLLAPSING).addClass(CLASS_NAME_COLLAPSE + " " + CLASS_NAME_SHOW);
195
        _this._element.style[dimension] = '';
196
197
        _this.setTransitioning(false);
198
199
        $__default['default'](_this._element).trigger(EVENT_SHOWN);
200
      };
201
202
      var capitalizedDimension = dimension[0].toUpperCase() + dimension.slice(1);
203
      var scrollSize = "scroll" + capitalizedDimension;
204
      var transitionDuration = Util__default['default'].getTransitionDurationFromElement(this._element);
205
      $__default['default'](this._element).one(Util__default['default'].TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
206
      this._element.style[dimension] = this._element[scrollSize] + "px";
207
    };
208
209
    _proto.hide = function hide() {
210
      var _this2 = this;
211
212
      if (this._isTransitioning || !$__default['default'](this._element).hasClass(CLASS_NAME_SHOW)) {
213
        return;
214
      }
215
216
      var startEvent = $__default['default'].Event(EVENT_HIDE);
217
      $__default['default'](this._element).trigger(startEvent);
218
219
      if (startEvent.isDefaultPrevented()) {
220
        return;
221
      }
222
223
      var dimension = this._getDimension();
224
225
      this._element.style[dimension] = this._element.getBoundingClientRect()[dimension] + "px";
226
      Util__default['default'].reflow(this._element);
227
      $__default['default'](this._element).addClass(CLASS_NAME_COLLAPSING).removeClass(CLASS_NAME_COLLAPSE + " " + CLASS_NAME_SHOW);
228
      var triggerArrayLength = this._triggerArray.length;
229
230
      if (triggerArrayLength > 0) {
231
        for (var i = 0; i < triggerArrayLength; i++) {
232
          var trigger = this._triggerArray[i];
233
          var selector = Util__default['default'].getSelectorFromElement(trigger);
234
235
          if (selector !== null) {
236
            var $elem = $__default['default']([].slice.call(document.querySelectorAll(selector)));
237
238
            if (!$elem.hasClass(CLASS_NAME_SHOW)) {
239
              $__default['default'](trigger).addClass(CLASS_NAME_COLLAPSED).attr('aria-expanded', false);
240
            }
241
          }
242
        }
243
      }
244
245
      this.setTransitioning(true);
246
247
      var complete = function complete() {
248
        _this2.setTransitioning(false);
249
250
        $__default['default'](_this2._element).removeClass(CLASS_NAME_COLLAPSING).addClass(CLASS_NAME_COLLAPSE).trigger(EVENT_HIDDEN);
251
      };
252
253
      this._element.style[dimension] = '';
254
      var transitionDuration = Util__default['default'].getTransitionDurationFromElement(this._element);
255
      $__default['default'](this._element).one(Util__default['default'].TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
256
    };
257
258
    _proto.setTransitioning = function setTransitioning(isTransitioning) {
259
      this._isTransitioning = isTransitioning;
260
    };
261
262
    _proto.dispose = function dispose() {
263
      $__default['default'].removeData(this._element, DATA_KEY);
264
      this._config = null;
265
      this._parent = null;
266
      this._element = null;
267
      this._triggerArray = null;
268
      this._isTransitioning = null;
269
    } // Private
270
    ;
271
272
    _proto._getConfig = function _getConfig(config) {
273
      config = _extends({}, Default, config);
274
      config.toggle = Boolean(config.toggle); // Coerce string values
275
276
      Util__default['default'].typeCheckConfig(NAME, config, DefaultType);
277
      return config;
278
    };
279
280
    _proto._getDimension = function _getDimension() {
281
      var hasWidth = $__default['default'](this._element).hasClass(DIMENSION_WIDTH);
282
      return hasWidth ? DIMENSION_WIDTH : DIMENSION_HEIGHT;
283
    };
284
285
    _proto._getParent = function _getParent() {
286
      var _this3 = this;
287
288
      var parent;
289
290
      if (Util__default['default'].isElement(this._config.parent)) {
291
        parent = this._config.parent; // It's a jQuery object
292
293
        if (typeof this._config.parent.jquery !== 'undefined') {
294
          parent = this._config.parent[0];
295
        }
296
      } else {
297
        parent = document.querySelector(this._config.parent);
298
      }
299
300
      var selector = "[data-toggle=\"collapse\"][data-parent=\"" + this._config.parent + "\"]";
301
      var children = [].slice.call(parent.querySelectorAll(selector));
302
      $__default['default'](children).each(function (i, element) {
303
        _this3._addAriaAndCollapsedClass(Collapse._getTargetFromElement(element), [element]);
304
      });
305
      return parent;
306
    };
307
308
    _proto._addAriaAndCollapsedClass = function _addAriaAndCollapsedClass(element, triggerArray) {
309
      var isOpen = $__default['default'](element).hasClass(CLASS_NAME_SHOW);
310
311
      if (triggerArray.length) {
312
        $__default['default'](triggerArray).toggleClass(CLASS_NAME_COLLAPSED, !isOpen).attr('aria-expanded', isOpen);
313
      }
314
    } // Static
315
    ;
316
317
    Collapse._getTargetFromElement = function _getTargetFromElement(element) {
318
      var selector = Util__default['default'].getSelectorFromElement(element);
319
      return selector ? document.querySelector(selector) : null;
320
    };
321
322
    Collapse._jQueryInterface = function _jQueryInterface(config) {
323
      return this.each(function () {
324
        var $element = $__default['default'](this);
325
        var data = $element.data(DATA_KEY);
326
327
        var _config = _extends({}, Default, $element.data(), typeof config === 'object' && config ? config : {});
328
329
        if (!data && _config.toggle && typeof config === 'string' && /show|hide/.test(config)) {
330
          _config.toggle = false;
331
        }
332
333
        if (!data) {
334
          data = new Collapse(this, _config);
335
          $element.data(DATA_KEY, data);
336
        }
337
338
        if (typeof config === 'string') {
339
          if (typeof data[config] === 'undefined') {
340
            throw new TypeError("No method named \"" + config + "\"");
341
          }
342
343
          data[config]();
344
        }
345
      });
346
    };
347
348
    _createClass(Collapse, null, [{
349
      key: "VERSION",
350
      get: function get() {
351
        return VERSION;
352
      }
353
    }, {
354
      key: "Default",
355
      get: function get() {
356
        return Default;
357
      }
358
    }]);
359
360
    return Collapse;
361
  }();
362
  /**
363
   * ------------------------------------------------------------------------
364
   * Data Api implementation
365
   * ------------------------------------------------------------------------
366
   */
367
368
369
  $__default['default'](document).on(EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
370
    // preventDefault only for <a> elements (which change the URL) not inside the collapsible element
371
    if (event.currentTarget.tagName === 'A') {
372
      event.preventDefault();
373
    }
374
375
    var $trigger = $__default['default'](this);
376
    var selector = Util__default['default'].getSelectorFromElement(this);
377
    var selectors = [].slice.call(document.querySelectorAll(selector));
378
    $__default['default'](selectors).each(function () {
379
      var $target = $__default['default'](this);
380
      var data = $target.data(DATA_KEY);
381
      var config = data ? 'toggle' : $trigger.data();
382
383
      Collapse._jQueryInterface.call($target, config);
384
    });
385
  });
386
  /**
387
   * ------------------------------------------------------------------------
388
   * jQuery
389
   * ------------------------------------------------------------------------
390
   */
391
392
  $__default['default'].fn[NAME] = Collapse._jQueryInterface;
393
  $__default['default'].fn[NAME].Constructor = Collapse;
394
395
  $__default['default'].fn[NAME].noConflict = function () {
396
    $__default['default'].fn[NAME] = JQUERY_NO_CONFLICT;
397
    return Collapse._jQueryInterface;
398
  };
399
400
  return Collapse;
401
402
})));
403
//# sourceMappingURL=collapse.js.map
404